package cn.walls1717.wallsmall.order.listener;

import cn.walls1717.wallsmall.common.pojo.to.mq.SeckillOrderTO;
import cn.walls1717.wallsmall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;

import static cn.walls1717.wallsmall.common.constant.order.OrderRabbitConstant.ORDER_SECKILL_OTHER_QUEUE;

/**
 * @author walls1717
 * @since 2022/11/27 16:08
 **/
@Slf4j
@Service
@RabbitListener(queues = ORDER_SECKILL_OTHER_QUEUE)
public class OrderSeckillListener {

    @Resource
    private OrderService orderService;

    /** 关闭过期订单 */
    @RabbitHandler
    public void listener(SeckillOrderTO seckillOrderTO, Channel channel, Message message) throws IOException {
        log.info("创建秒杀订单详细信息 -> {}", seckillOrderTO.getOrderSn());
        try {
            orderService.addSeckillOrder(seckillOrderTO);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            e.printStackTrace();
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }
}
